import React from 'react'
import { BrowserRouter as Router, Route, Switch, Redirect } from 'react-router-dom'
import Loadable from 'react-loadable'
import App from '../views/Layout/index'
import Login from '../views/Login/LoginForm'
import LoadingComponent from '../components/Common/LoadingComponent'

const routers = [
    {
        path: '/login', //登陆
        component: () => import('../views/Login/LoginForm')
    },
    {
        path: '/password', //修改密码
        component: () => import('../views/Login/Passwords')
    },
    {
        path: '/person/list', //人事管理
        component: () => import('../views/Person/ManagerList')
    },
    {
        path: '/person/rightlist',
        component: () => import('../views/Person/RightList')
    },
    {
        path: '/business/list', // 商家管理
        component: () => import('../views/Business/MerchantsList')
    },
    {
        path: '/business/detail/:id(\\d+)',
        component: () => import('../views/Business/MerchantsDetail')
    },
    {
        path: '/business/stores',
        component: () => import('../views/Business/StoresList')
    },
    {
        path: '/goods/cat/list', // 品类管理
        component: () => import('../views/Goods/ClassifyManageIndex')
    },
    {
        path: '/goods/list', // 商品管理
        component: () => import('../views/Goods/List')
    },
    {
        path: '/goods/detail/select',
        component: () => import('../views/Goods/Detail/Select')
    },
    {
        path: '/goods/detail/index',
        component: () => import('../views/Goods/Detail/Index')
    },
    {
        path: '/passwords/modify',  // 修改密码
        component: () => import('../views/Passwords/ModifyPasswords')
    }
]

const token = sessionStorage.getItem("token")
// if (!token) {
//     window.location.href = '/login'
// }
class RouteMap extends React.Component {

  render () {
      if (token) {
          return (
              <Router>
                  <App>
                      <Switch>
                          <Route exact path="/" render={() => (<Redirect to="/login" />)} />
                          {
                              routers.map(({ path, component }, key) => (
                                  <Route
                                      key={key}
                                      exact
                                      path={path}
                                      component={Loadable({
                                          loader: component,
                                          loading: LoadingComponent
                                      })}
                                  />
                              ))
                          }
                      </Switch>
                  </App>
              </Router>
          )
      } else {
          return (
              <Router>
                  <Login ref="login"></Login>
              </Router>

          )
      }

  }
}

export default RouteMap
